package cn.sm1234.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class GlobalExceptionHandler {
    // java.lang.ArithmeticException
    @ExceptionHandler(value = {ArithmeticException.class})
    public ModelAndView handlerArithmeticException(Exception e){
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", "global handler:" + e.toString());
        mv.setViewName("error1");
        return mv;
    }

    // java.lang.NullPointerException
    @ExceptionHandler(value = {NullPointerException.class})
    public ModelAndView handlerNullPointerException(Exception e){
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", "global handler:" +  e.toString());
        mv.setViewName("error2");
        return mv;
    }
}
